package com.ldatum.crm.action.admin;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.ldatum.crm.domain.Admin;
import com.ldatum.crm.domain.TContacts;
import com.ldatum.crm.domain.TCustomer;
import com.ldatum.crm.domain.TEmployee;
import com.ldatum.crm.domain.TServices;
import com.ldatum.crm.domain.TTracks;
import com.ldatum.crm.service.TCustomerService;
import com.ldatum.crm.service.TEmployeeService;
import com.ldatum.crm.service.TServicesService;
import com.ldatum.crm.serviceImpl.TCustomerServiceImpl;
import com.ldatum.crm.serviceImpl.TEmployeeServiceImpl;
import com.ldatum.crm.serviceImpl.TServicesServiceImpl;

@ParentPackage("admin")

public class ServicesAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private TTracks tracks = new TTracks();
	private TServices service = new TServices();
	
	List<TServices> servicesList = new ArrayList<TServices>();
	List<TServices> servicesListByMe = new ArrayList<TServices>();
	List<TServices> servicesListToMe = new ArrayList<TServices>();
	List<TCustomer> customerList = new ArrayList<TCustomer>();
	List<TEmployee> employeeList = new ArrayList<TEmployee>();
	
	TServicesService servicesService = new TServicesServiceImpl();
	TCustomerService customerService = new TCustomerServiceImpl();
	TEmployeeService employeeService = new TEmployeeServiceImpl();
	
	private Integer id;
	private TCustomer customer;
	private TContacts contacts;
	private String telephone;
	private String requirements;
	private String serviceMode;
	private String cannotSatisfyOfRequirements;
	private String featuresOfInterest;
	private String systemType;
	private String serviceDetail;
	private String nextPlan;
	private String creater;
	private String createTime;
	private String servicer;
	private String finishTime;
	private String nextPlanDetail;
	private String status;
	private String serviceDuration;
	private int customerId;
	private int contactsId;
	
	private String levelOne;
	private String levelTwo;
	
	public void findById(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			service = servicesService.findById(id);
			json.accumulate("service", service);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	public void save(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			service = setService();
			servicesService.save(service);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	

	public void update(){
		PrintWriter out = null;
		JSONObject json = new JSONObject();
		try{
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("type/html");
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			service = setService();
			servicesService.update(service);
			json.accumulate("result", true);
		}catch(Exception e){
			e.printStackTrace();
			json.accumulate("result", false);
		}finally{
			out.println(json.toString());
			out.flush();
			out.close();
		}
	}
	
	public String list() throws Exception{
		String creater = (String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME);
		servicesListByMe = servicesService.listByMe(TServices.class,creater);
		servicesListToMe = servicesService.listToMe(TServices.class,creater);
		levelOne = "customer";
		levelTwo = "services";
		return "listServices";
	}
//	@Test
	public String add() throws Exception{
		try {
			String currentEmployee = (String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME);
			creater = currentEmployee;
			createTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
			customerList = customerService.myCustomer(TCustomer.class,currentEmployee); //我的客户列表
			employeeList = employeeService.list(TEmployee.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		levelOne = "customer";
		levelTwo = "services";
		return "addService";
	}

	public String edit() throws Exception{
		try {
			String currentEmployee = (String)getSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME);
			service = servicesService.findById(id);
			customerList = customerService.myCustomer(TCustomer.class,currentEmployee); //我的客户列表
			employeeList = employeeService.list(TEmployee.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		levelOne = "customer";
		levelTwo = "services";
		return "editService";
	}
	
	public TTracks getTracks() {
		return tracks;
	}

	public void setTracks(TTracks tracks) {
		this.tracks = tracks;
	}

	public void getService(TServices service) {
		cannotSatisfyOfRequirements = service.getCannotSatisfyOfRequirements();
		contacts = service.getContacts();
		creater = service.getCreater();
		createTime = service.getCreateTime();
		customer = service.getCustomer();
		featuresOfInterest = service.getFeaturesOfInterest();
		finishTime = service.getFinishTime();
		nextPlan = service.getNextPlan();
		nextPlanDetail = service.getNextPlanDetail();
		serviceDuration = service.getServiceDuration();
		serviceMode = service.getServiceMode();
		servicer = service.getServicer();
		status = service.getStatus();
		systemType = service.getSystemType();
		telephone = service.getTelephone();
		requirements = service.getRequirements();
		serviceDetail = service.getServiceDetail();
	}

	public TServices setService() {
		TServices ser = new TServices();
		ser.setCannotSatisfyOfRequirements(cannotSatisfyOfRequirements);
		TContacts con = new TContacts();
		con.setId(contactsId);
		TCustomer cus = new TCustomer();
		cus.setId(customerId);
		ser.setContacts(con);
		ser.setCreater(creater);
		ser.setCreateTime(createTime);
		ser.setCustomer(cus);
		ser.setFeaturesOfInterest(featuresOfInterest);
		ser.setFinishTime(finishTime);
		ser.setNextPlan(nextPlan);
		ser.setNextPlanDetail(nextPlanDetail);
		ser.setServiceDuration(serviceDuration);
		ser.setServiceMode(serviceMode);
		ser.setServicer(servicer);
		ser.setStatus(status);
		ser.setSystemType(systemType);
		ser.setTelephone(telephone);
		ser.setRequirements(requirements);
		ser.setServiceDetail(serviceDetail);
		ser.setId(id);
		return ser;
	}

	public List<TServices> getServicesList() {
		return servicesList;
	}

	public void setServicesList(List<TServices> servicesList) {
		this.servicesList = servicesList;
	}

	public List<TCustomer> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<TCustomer> customerList) {
		this.customerList = customerList;
	}

	public List<TEmployee> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List<TEmployee> employeeList) {
		this.employeeList = employeeList;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public TServices getService() {
		return service;
	}

	public void setService(TServices service) {
		this.service = service;
	}

	public TCustomer getCustomer() {
		return customer;
	}

	public void setCustomer(TCustomer customer) {
		this.customer = customer;
	}

	public TContacts getContacts() {
		return contacts;
	}

	public void setContacts(TContacts contacts) {
		this.contacts = contacts;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getRequirements() {
		return requirements;
	}

	public void setRequirements(String requirements) {
		this.requirements = requirements;
	}

	public String getServiceMode() {
		return serviceMode;
	}

	public void setServiceMode(String serviceMode) {
		this.serviceMode = serviceMode;
	}

	public String getCannotSatisfyOfRequirements() {
		return cannotSatisfyOfRequirements;
	}

	public void setCannotSatisfyOfRequirements(String cannotSatisfyOfRequirements) {
		this.cannotSatisfyOfRequirements = cannotSatisfyOfRequirements;
	}

	public String getFeaturesOfInterest() {
		return featuresOfInterest;
	}

	public void setFeaturesOfInterest(String featuresOfInterest) {
		this.featuresOfInterest = featuresOfInterest;
	}

	public String getSystemType() {
		return systemType;
	}

	public void setSystemType(String systemType) {
		this.systemType = systemType;
	}

	public String getServiceDetail() {
		return serviceDetail;
	}

	public void setServiceDetail(String serviceDetail) {
		this.serviceDetail = serviceDetail;
	}

	public String getNextPlan() {
		return nextPlan;
	}

	public void setNextPlan(String nextPlan) {
		this.nextPlan = nextPlan;
	}

	public String getCreater() {
		return creater;
	}

	public void setCreater(String creater) {
		this.creater = creater;
	}

	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getServicer() {
		return servicer;
	}

	public void setServicer(String servicer) {
		this.servicer = servicer;
	}

	public String getFinishTime() {
		return finishTime;
	}

	public void setFinishTime(String finishTime) {
		this.finishTime = finishTime;
	}

	public String getNextPlanDetail() {
		return nextPlanDetail;
	}

	public void setNextPlanDetail(String nextPlanDetail) {
		this.nextPlanDetail = nextPlanDetail;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getServiceDuration() {
		return serviceDuration;
	}

	public void setServiceDuration(String serviceDuration) {
		this.serviceDuration = serviceDuration;
	}

	public List<TServices> getServicesListByMe() {
		return servicesListByMe;
	}

	public void setServicesListByMe(List<TServices> servicesListByMe) {
		this.servicesListByMe = servicesListByMe;
	}

	public List<TServices> getServicesListToMe() {
		return servicesListToMe;
	}

	public void setServicesListToMe(List<TServices> servicesListToMe) {
		this.servicesListToMe = servicesListToMe;
	}

	public int getCustomerId() {
		return customerId;
	}

	public void setCustomerId(int customerId) {
		this.customerId = customerId;
	}

	public int getContactsId() {
		return contactsId;
	}

	public void setContactsId(int contactsId) {
		this.contactsId = contactsId;
	}

	public String getLevelOne() {
		return levelOne;
	}

	public void setLevelOne(String levelOne) {
		this.levelOne = levelOne;
	}

	public String getLevelTwo() {
		return levelTwo;
	}

	public void setLevelTwo(String levelTwo) {
		this.levelTwo = levelTwo;
	}
	
	
	
}
